<?php
include './vendor/autoload.php';

use Illuminate\Container\Container;
$container = Container::getInstance();

interface MailInterface
{
    public function send($receive, $content);
}
interface LogInterface
{
    public function write($filename, $content);
}
class RedisLog implements LogInterface
{
    public function __construct()
    {
        echo "Redis做日志 init<br>";
    }
    public function write($filename, $content)
    {
        echo "redis存储成功";
    }
}


class GmailMail implements MailInterface
{
    private $log;

    public function __construct(LogInterface $log)
    {
        echo "Gmail 初始化 <br>";
        $this->log = $log;
    }
    public function send($receive, $content)
    {
        printf('我给%s发了邮件,内容是：%s', $receive,$content) ;
    }
}

$container->bind(MailInterface::class, GmailMail::class);
$container->bind(LogInterface::class, RedisLog::class);
//$container->make(MailInterface::class);

class FileLog implements LogInterface
{
    public function __construct()
    {
        echo '本地文件做日志<br>';
    }
    public function write($filename, $content)
    {
        echo "存储到本地文件";
    }
}

$container->bind(LogInterface::class, FileLog::class);
$gmail = $container->make(MailInterface::class);
$gmail->send('529303133@qq.com', '注册成功<br>');

$container->bind(LogInterface::class, function (){
    echo "我是闭包函数<br>";
});

$container->resolving(LogInterface::class, function (){
    echo "这个函数会在绑定的类解析完成之后调用<br>";
});

$container->make(LogInterface::class);
$container->make(LogInterface::class);

$container->alias(LogInterface::class, 'Log');
$container->make('Log');


echo "<br>";
$ifHasBound = $container->bound(LogInterface::class);
var_dump($ifHasBound);

echo "<br>";
$ifHas = $container->has(LogInterface::class);
$ifHas2 = $container->has('LogInterface');
var_dump($ifHas);
var_dump($ifHas2);

echo "<br>";
$resolved = $container->resolved(LogInterface::class);
var_dump($resolved);

echo "<br>";
$isShared = $container->isShared(LogInterface::class);
var_dump($isShared);
echo "<br>";
$isAlias = $container->isAlias('Log');
var_dump($isAlias);


$container['Log'];
echo "Container 实现了 PHP 的 ArrayAccess 接口<br>";



/**
 * 框架里的resolve()调用的app(), app()又是调用的$container->make()。
 * $container->make() 是唯一公开的(解析实出实例)方法。
 *
 * bind()是用来 注册container的bindings的
 *
 * 翻译：Determine if      -       判断是否
 */




